<?php
require_once('../config/config.inc.php');
require_once('class.baseobject.php');
require_once('class.shift.php');
require_once('class.report.php');
require_once('reportfns.php');
ob_start();
$title = 'Monthly Sales Summary';
$thismonth = isset($_POST['thismonth']) ? $_POST['thismonth'] : date('m');
$thisyear = isset($_POST['thisyear']) ? $_POST['thisyear'] : date('Y');


//functions
function getMonth($selected) {
	$ret = "<select name='thismonth' id='thismonth'>";
	for($x=1; $x <=12; $x++) {
		$month = date('F', strtotime('2010-' . $x . '-01'));
		$ret.="<option value='$x' ";
		if($selected==$x) $ret.=" selected ";
		$ret.=">$month</option>";
	}
	$ret.="</select>";
	return $ret;
}

function getYear($selected) {
	$ret = "<select name='thisyear' id='thisyear'>";
	for($x=2010; $x <=2015; $x++) {
		$ret.="<option value='$x' ";
		if($selected==$x) $ret.=" selected ";
		$ret.=">$x</option>";
	}
	$ret.="</select>";
	return $ret;
}

function getSales($date,$category,$type=null,$site=null) {
	$sql  = " select sum($category) from salessummary 
			  where salesdate='$date'  ";
	if(!is_null($type) && !is_null($site)) $sql.=" and type='$type' and site='$site' ";
	$res = mysql_query($sql) or die($sql);
	$row = mysql_fetch_row($res);
	return $row[0];
}

function getTotalSales($date,$fields,$type=null,$site=null) {
	$sql  = " select sum($fields) from salessummary 
			  where salesdate='$date'  ";
	if(!is_null($type) && !is_null($site)) $sql.=" and type='$type' and site='$site' ";
	$res = mysql_query($sql) or die($sql);
	$row = mysql_fetch_row($res);
	return $row[0];
}
?>
<?php

$table="<table border='1'>
<tr>
<th rowspan=2 colspan=2>Day</th>
<th colspan=5>Grand Totals</th>
<th rowspan=2 class='grand'>Avg</th>
<th rowspan=2 class='grand'>Cum</th>

<th colspan=4>Room</th>
<th colspan=4>Overtime</th>
<th rowspan=2 class='grand'>Room<br />Sales</th>

<th colspan=4>Food</th>
<th colspan=4>Beer</th>
<th rowspan=2>Banquet<br />Sales</th>
<th rowspan=2 class='grand'>Kitchen<br />Sales</th>

<th colspan=4>Misc</th>

</tr>
<tr>
<th>Guests</th>
<th>Walkup</th>
<th>Hotel</th>
<th>Shogun2</th>
<th>Total</th>

<th>Walkup</th>
<th>Hotel</th>
<th>Shogun2</th>
<th>Total</th>

<th>Walkup</th>
<th>Hotel</th>
<th>Shogun2</th>
<th>Total</th>

<th>Walkup</th>
<th>Hotel</th>
<th>Shogun2</th>
<th>Total</th>

<th>Walkup</th>
<th>Hotel</th>
<th>Shogun2</th>
<th>Total</th>

<th>Walkup</th>
<th>Hotel</th>
<th>Shogun2</th>
<th>Total</th>
</tr>
";
?>
<?php
$cumulative = '';
$avg = '';
for($x=1; $x < 32; $x++) {
	if(strlen($x)==1) $x = '0'.$x;
	$date = $thisyear.'-'.$thismonth.'-'.$x;
	$day = date('D',strtotime($date));
	$class='';
	if($day=='Sun' or $day=='Sat') $class='class="weekend"';
	$rows .= "<tr $class>";
	$rows .= "<td>$x</td>";
	$rows .= "<td>" . $day ."</td>";
	
	$rows .= "<td>" .getSales($date,'guests'). "</td>";
	$rows .= "<td>" .getTotalSales($date,'roomsales + overtime + food + beer + misc',1,1). "</td>";
	$rows .= "<td>" .getTotalSales($date,'roomsales + overtime + food + beer + misc',2,1). "</td>";
	$rows .= "<td>" .getTotalSales($date,'roomsales + overtime + food + beer + misc',2,2). "</td>";
	$total = getTotalSales($date,'roomsales + overtime + food + beer + misc');
	$rows .= "<td>" .$total. "</td>";
	if($total) {
		$prev = $avg; 
		$cumulative +=$total;
		$avg = ceil($cumulative/$x );
		$rows .= "<th class='grand'>" .number_format($avg,0). "</td>";
		$rows .= "<th class='grand'>" .number_format($cumulative,0). "</td>";
		
	}else{
		$rows .= "<td></td>";
		$rows .= "<td></td>";
	}
	
	
	$rows .= "<td>" .getSales($date,'roomsales',1,1). "</td>";
	$rows .= "<td>" .getSales($date,'roomsales',2,1). "</td>";
	$rows .= "<td>" .getSales($date,'roomsales',2,2). "</td>";
	$rows .= "<td>" .getSales($date,'roomsales'). "</td>";
	
	$rows .= "<td>" .getSales($date,'overtime',1,1). "</td>";
	$rows .= "<td>" .getSales($date,'overtime',2,1). "</td>";
	$rows .= "<td>" .getSales($date,'overtime',2,2). "</td>";
	$rows .= "<td>" .getSales($date,'overtime'). "</td>";
	
	$rows .= "<th class='grand'>" .getTotalSales($date,'roomsales + overtime '). "</td>";
	
	$rows .= "<td>" .getSales($date,'food',1,1). "</td>";
	$rows .= "<td>" .getSales($date,'food',2,1). "</td>";
	$rows .= "<td>" .getSales($date,'food',2,2). "</td>";
	$rows .= "<td>" .getSales($date,'food'). "</td>";
	
	$rows .= "<td>" .getSales($date,'beer',1,1). "</td>";
	$rows .= "<td>" .getSales($date,'beer',2,1). "</td>";
	$rows .= "<td>" .getSales($date,'beer',2,2). "</td>";
	$rows .= "<td>" .getSales($date,'beer'). "</td>";
	
	$rows .= "<td>" .getSales($date,'banquet'). "</td>";
	$rows .= "<th class='grand'>" .getTotalSales($date,' food + beer + banquet'). "</td>";
	
	$rows .= "<td>" .getSales($date,'misc',1,1). "</td>";
	$rows .= "<td>" .getSales($date,'misc',2,1). "</td>";
	$rows .= "<td>" .getSales($date,'misc',2,2). "</td>";
	$rows .= "<td>" .getSales($date,'misc'). "</td>";
	
	
	
	$rows .= '</tr>';
}

$table.=$rows . "</table>";

if($_POST['submit']=='export to excel') {
	$export_file = "monthlysummary{$thisyear}-{$thismonth}.xls";
    //ini_set('zlib.output_compression','Off');
    header('Pragma: public');
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");                  // Date in the past   
    header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');     // HTTP/1.1
    header('Cache-Control: pre-check=0, post-check=0, max-age=0');    // HTTP/1.1
    header ("Pragma: no-cache");
    header("Expires: 0");
    header('Content-Transfer-Encoding: none');
    //header('Content-Type: application/vnd.ms-excel;');                 // This should work for IE & Opera
    //header("Content-type: application/x-msexcel");                    // This should work for the rest
    header('Content-Disposition: attachment; filename="'.basename($export_file).'"'); 
	echo $table;
}else{
?>
<html>
<head>
<link rel='stylesheet' type='text/css' href='./reports.css'>
<link rel='stylesheet' type='text/css' href='../../css/start/jquery-ui.css'>
<script type='text/javascript' src='../../js/jquery.js'></script>
<script type='text/javascript' src='../../js/jquery-ui.js'></script>
<style>
table {
	border-collapse:collapse;
}
table th,td {
	padding:4px;
	text-align:center;
}

th.grand, td.grand {
	background-color:#eeffcc;
}

tr.weekend {
	background-color:#ffeecc;
}
</style>
</head>
<body>
<form method='post'>
<div>Select Month: <?php echo getmonth($thismonth) . getyear($thisyear) ; ?>
<input type='submit' name='submit' value='go' />
<input type='submit' name='submit' value='export to excel' />
</div>
<div id='workpanel'>
<?php echo $table; ?>
</div>
</form>
<script>
$(document).ready(function(){
	$('#newdate').datepicker({dateFormat:'yy-mm-dd'});
});
</script>
</body>
</html>
<?php
}
?>
